package com.sunwayworld.basemodule.common.eptstorage.file.excelread.service.impl;

import com.sunwayworld.basemodule.common.clickhouse.client.ClickHouseConnection;
import com.sunwayworld.basemodule.common.clickhouse.utils.ClickHouseBaseUtil;
import com.sunwayworld.basemodule.common.eptstorage.file.excelread.listener.ExcelRowData;
import com.sunwayworld.basemodule.common.eptstorage.file.excelread.listener.SheetData;
import com.sunwayworld.basemodule.common.eptstorage.file.excelread.service.IExcelDataStorageService;
import com.sunwayworld.framework.spring.annotation.GikamBean;
import com.sunwayworld.framework.utils.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther zhangs
 * @description
 * @date 2022/1/18
 */
@Service
@GikamBean
public class ClickHouseExcelDataStorageService implements IExcelDataStorageService {

    @Autowired
    public ClickHouseConnection clickHouseConnection;

    @Override
    public List<Map<String, Object>> storage(List<SheetData> sheetDataList, Map<String, Object> businessMap) {
        List<Map<String, Object>> resultMaps = new ArrayList<>();


        // 删除历史上传的数据
        for (SheetData sheetData : sheetDataList) {

            String document = sheetData.getMongoDocument();
            if (businessMap != null) {
                Object matrixId = businessMap.get("matrixId");
                if (matrixId != null) {

                    String sql = "alter table "+ document +" delete where matrixId =  '" + matrixId + "'";
                    ClickHouseBaseUtil.deleteBySql(clickHouseConnection, sql);
                }
            }
        }

        // 插入新的原始数据
        for (SheetData sheetData : sheetDataList) {
            List<String> columns = new ArrayList<>();
            sheetData.getTitleFieldMap().forEach((colIndex,field)->{
                columns.add(field);
            });
            List<Map<String, Object>> maps = this.putToClickHouse(sheetData, sheetData.getMongoDocument(), businessMap, columns);
            if (maps != null) {
                resultMaps.addAll(maps);
            }
        }
        return resultMaps;
    }

    public List<Map<String, Object>> putToClickHouse(SheetData sheetData, String document, Map<String, Object> businessMap, List<String> columns) {
        List<ExcelRowData> rowDataList = sheetData.getRowDataList();
        if(ObjectUtils.isEmpty(sheetData) || ObjectUtils.isEmpty(rowDataList)){
            return null;
        }
        int rowSize = rowDataList.size();
        List<Map<String, Object>> resultMaps = new ArrayList<>();
        for (int i = 0; i < rowSize; i++) {
            LinkedHashMap<String, Object> values = rowDataList.get(i).getValues();
            values.put("matrixId", businessMap.get("matrixId"));
            resultMaps.add(values);
        }
        columns.add("matrixId");
        ClickHouseBaseUtil.batchInsert(clickHouseConnection,resultMaps,document,columns);
        return resultMaps;
    }
}
